package fei.yuzhi.com.loveflowers.fragment.firstpagefragment.autoscroll;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.bean.firstpageautobean.ItemBean;
import fei.yuzhi.com.loveflowers.fragment.firstpagefragment.detail.DetailActivity;

/**
 * Created by My on 2016/10/7.
 */
public class AutoScrollJumpActivity extends AppCompatActivity implements AutoScrollJumpContract
        .AutoScrollView {
    @BindView(R.id.firstpage_autoscroll_back)
    ImageView firstpageAutoscrollBack;
    @BindView(R.id.firstpage_autoscroll_gv)
    GridView firstpageAutoscrollGv;
    @BindView(R.id.firstpage_autoscroll_title)
    TextView firstpageAutoscrollTitle;
    private AutoScrollJumpPresenter presenter;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autoscrolljumpto);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String data = intent.getStringExtra("data");
        Log.i("tag", "auto  data" + data);
        presenter = new AutoScrollJumpPresenter(this);
        presenter.getGridDataFromNet(data);
        firstpageAutoscrollBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onGridViewSuccess(final List<ItemBean> itemBeanList, final String title) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                //将得到的数据加载到适配器中
                AutoScrollGridAdapter adapter = new AutoScrollGridAdapter(AutoScrollJumpActivity
                        .this, itemBeanList);
                firstpageAutoscrollGv.setAdapter(adapter);
                //绑定girdview的监听事件
                firstpageAutoscrollGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long
                            id) {
                        Intent intent = new Intent(AutoScrollJumpActivity.this, DetailActivity
                                .class);
                        intent.putExtra("data", itemBeanList.get(position).getGoods_id());
                        startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    public void onGridViewFail(String msg) {
        Toast.makeText(AutoScrollJumpActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
}
